<template>
  <div class="container">
    <h1>卡牌分类管理</h1>
    <div class="header">
      <span class="stats">现有卡牌{{ totalItems }}张</span>
      <button class="add-btn" @click="openDialog('add')">+ 添加卡牌</button>
    </div>

    <!-- 弹窗 -->
    <div class="dialog-mask" v-if="showDialog">
      <div class="dialog-container">
        <h3>{{ isEditing ? '编辑卡牌' : '添加新卡牌' }}</h3>
        <form @submit.prevent="handleSubmit">
          <div class="form-group">
            <label>卡密</label>
            <input v-model="form.keyword" required class="form-input">
          </div>
          <div class="form-group">
            <label>卡牌名称</label>
            <input v-model="form.cardname" required class="form-input">
          </div>
          <div class="form-group">
            <label>罕贵度</label>
            <select v-model="form.rarity" required class="form-select">
              <option value="">请选择</option>
              <option>平卡</option>
              <option>银字</option>
              <option>面闪</option>
              <option>金闪</option>
              <option>红碎</option>
            </select>
          </div>
          <div class="form-group">
            <label>来源</label>
            <select v-model="form.origin" class="form-select">
              <option value="">请选择</option>
              <option>简中</option>
              <option>日文</option>
              <option>亚英</option>
              <option>其他</option>
            </select>
          </div>
          <div class="form-group">
            <label>卡盒批次</label>
            <input v-model="form.batch" class="form-input">
          </div>
          <div class="dialog-actions">
            <button type="button" class="cancel-btn" @click="closeDialog">取消</button>
            <button type="submit" class="confirm-btn">{{ isEditing ? '更新' : '添加' }}</button>
          </div>
        </form>
      </div>
    </div>

    <!-- 数据表格 -->
    <div class="table-wrapper">
      <table v-if="cards.length" class="card-table">
        <thead>
          <tr>
            <th>卡密</th>
            <th>卡牌名称</th>
            <th>罕贵度</th>
            <th>来源</th>
            <th>卡盒批次</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="card in cards" :key="card.id">
            <td>{{ card.keyword }}</td>
            <td>{{ card.cardname }}</td>
            <td><span class="rarity-tag">{{ card.rarity }}</span></td>
            <td>{{ card.origin || '-' }}</td>
            <td>{{ card.batch || '-' }}</td>
            <td>
              <router-link :to="{ name: 'CardDetailPage', params: { id: card.id } }" class="detail-btn">查看详情</router-link>
              <button class="edit-btn" @click="openDialog('edit', card)">编辑</button>
              <button class="delete-btn" @click="deleteCard(card.id)">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
      <div v-else class="empty-state">
        <div class="empty-icon">∅</div>
        <p>暂无卡牌数据</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { 
  getAllCardmates, 
  addCardmate, 
  updateCardmate, 
  deleteCardmate 
} from '@/api/Cardmate'

const cards = ref([])
const showDialog = ref(false)
const isEditing = ref(false)
const currentId = ref(null)

const form = reactive({
  keyword: '',
  cardname: '',
  rarity: '',
  origin: '',
  batch: ''
})

const totalItems = computed(() => cards.value.length)

onMounted(() => {
  loadData()
})

const loadData = async () => {
  try {
    const response = await getAllCardmates()
    cards.value = response.data.data
  } catch (error) {
    alert('加载数据失败：' + error.message)
  }
}

const openDialog = (mode, card = null) => {
  if (mode === 'edit' && card) {
    Object.assign(form, card)
    currentId.value = card.id
    isEditing.value = true
  } else {
    resetForm()
    isEditing.value = false
  }
  showDialog.value = true
}

const handleSubmit = async () => {
  try {
    if (isEditing.value) {
      await updateCardmate({ ...form, id: currentId.value })
    } else {
      await addCardmate(form)
    }
    await loadData()
    closeDialog()
  } catch (error) {
    alert('操作失败：' + error.message)
  }
}

const deleteCard = async (id) => {
  if (confirm('确定要删除这个卡牌吗？')) {
    try {
      await deleteCardmate(id)
      await loadData()
    } catch (error) {
      alert('删除失败：' + error.message)
    }
  }
}

const resetForm = () => {
  Object.keys(form).forEach(key => form[key] = '')
  currentId.value = null
}

const closeDialog = () => {
  showDialog.value = false
  resetForm()
}
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
  font-family: system-ui, -apple-system, sans-serif;
}

h1 {
  color: #1a1a1a;
  font-size: 1.8rem;
  margin-bottom: 2rem;
  padding-bottom: 0.75rem;
  border-bottom: 2px solid #eee;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  padding: 1rem;
  background: #f8f9fa;
  border-radius: 8px;
}

.stats {
  color: #666;
  font-size: 0.95rem;
}

.add-btn {
  background: #2172ff;
  color: white;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 0.95rem;
}

.add-btn:hover {
  background: #1a5dd8;
  box-shadow: 0 2px 6px rgba(33, 114, 255, 0.2);
}

.table-wrapper {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.card-table {
  width: 100%;
  border-collapse: collapse;
  background: white;
}

.card-table th {
  background: #f8f9fa;
  color: #606266;
  font-weight: 600;
  padding: 1rem;
  text-align: left;
  border-bottom: 2px solid #e4e7ed;
}

.card-table td {
  padding: 0.8rem 1rem;
  color: #606266;
  border-bottom: 1px solid #e4e7ed;
}

.card-table tr:hover {
  background-color: #f5f7fa;
}

.rarity-tag {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  background: #f0f2f5;
  font-size: 0.85rem;
}

.edit-btn {
  background: #2172ff;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 0.4rem 0.8rem;
  margin-right: 0.5rem;
  cursor: pointer;
  transition: background 0.2s;
}

.edit-btn:hover {
  background: #1a5dd8;
}

.delete-btn {
  background: #ff4d4f;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  transition: background 0.2s;
}

.delete-btn:hover {
  background: #d9363e;
}

.empty-state {
  padding: 3rem 1rem;
  text-align: center;
  background: white;
}

.empty-icon {
  font-size: 2.5rem;
  color: #c0c4cc;
  margin-bottom: 1rem;
}

.empty-state p {
  color: #909399;
  font-size: 0.95rem;
}

.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-container {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  width: 500px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.dialog-container h3 {
  margin: 0 0 1.5rem;
  font-size: 1.2rem;
  color: #1a1a1a;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #606266;
  font-size: 0.9rem;
}

.form-input,
.form-select {
  width: 100%;
  padding: 0.6rem;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 0.95rem;
  transition: border-color 0.2s;
}

.form-input:focus,
.form-select:focus {
  border-color: #2172ff;
  outline: none;
  box-shadow: 0 0 0 2px rgba(33, 114, 255, 0.1);
}

.dialog-actions {
  margin-top: 1.5rem;
  display: flex;
  justify-content: flex-end;
  gap: 0.8rem;
}

.cancel-btn {
  background: #f0f2f5;
  color: #606266;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s;
}

.cancel-btn:hover {
  background: #e4e7ed;
}

.confirm-btn {
  background: #2172ff;
  color: white;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s;
}

.confirm-btn:hover {
  background: #1a5dd8;
}

/* 新加入的路由链接 */
/* 添加到现有的 <style scoped> 标签内 */
.detail-btn {
  display: inline-block;
  background: #42b983; /* 你可以选择你喜欢的颜色 */
  color: white;
  padding: 0.4rem 0.5rem; /* 减少填充 */
  margin-right: 0.5rem;
  border: none;
  border-radius: 4px;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.2s;
  font-size: 0.9rem; /* 减小字体大小 */
}

.detail-btn:hover {
  background: #369973; /* 深一点的绿色，鼠标悬停时显示 */
}


</style>